package fatclientnbp;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import Model.Trip;
import edu.chl.chalmers.fatclient.sb.Flight;
import javax.swing.*;
import java.sql.*;


public class ResultPanel extends JPanel implements ActionListener{


	private JButton book;
        private ArrayList<JButton> buttonList;
        private ArrayList<JTable> tableList;
        private ArrayList<JPanel> panelList;
        private JPanel panel;        
        private TreeSet<Trip> trips;
        private HashMap<String, Trip> buttonMap;
        private Trip chosenTrip;
        private MainController controller;
        
	public ResultPanel(TreeSet<Trip> trips,MainController controller)
	{
            this.controller = controller;
            //this.setPreferredSize(new Dimension (400, 400));
            this.trips=trips;
            panel=new JPanel();
            //panel=this;
            panelList=new ArrayList<JPanel>();
            buttonList=new ArrayList<JButton> ();
            tableList=new ArrayList<JTable>();
            buttonMap = new HashMap<String, Trip>();
            createTables();
            showTables();
       

        }
        	
	public void createTables()
	{
            
            String[] columns={"Airline", "Departure Airport", "Destination Airport", "Departure Time", "Destination Time", "Free Seats", "Price"};
            int j=0;
		for(Trip t: trips)
                {
                    
                   // DefaultTableModel model=new DefaultTableModel(7, t.getFlights().size()+1);       
                    JTable table=new JTable(t.getFlights().size()+1, 7);
                    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
                    
                    for(int k=0; k<columns.length; k++)
                    {
                        table.setValueAt(columns[k], 0, k);
                    }
                    table.getTableHeader().setBackground(Color.yellow);
                    
                    table.repaint();
                     table.getTableHeader().repaint();
                     table.getTableHeader().setBackground(Color.blue);
                    
                    JPanel panel=new JPanel();
                    panel.setLayout(new GridBagLayout());
                    GridBagConstraints c = new GridBagConstraints();
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx=0;
                    c.gridy =0;
                    int i=0;
                    for(Flight f: t.getFlights())
                    {
                        i++;
                        
                        table.setValueAt(f.getAirline().getName(), i, 0);
                        table.setValueAt(f.getDepartureAirport(), i, 1);
                        table.setValueAt(f.getDestinationAirport(), i, 2);
                        table.setValueAt(f.getDepartureTime(), i, 3);
                        table.setValueAt(f.getArrivalTime(), i, 4);
                        table.setValueAt(f.getFreeSeats().size(), i, 5);
                        table.setValueAt(f.getPrice(), i, 6);
                        table.setEnabled(false);
                    }

                   JButton button=new JButton("Book");
                   String buttonID="tripID: "+j;
                   button.setActionCommand(buttonID);
                   button.addActionListener(this);
                   buttonMap.put(buttonID, t);
                   JTextField field=new JTextField(t.getDepartureAirport()+"->"+t.getDestinationAirport()+"  Duration: "+new Time(System.currentTimeMillis()+t.getDuration()));
                   field.setEditable(false);
                   
                   panel.add(field,c);
                   
                   c.gridy++;
                   panel.add(table,c);
                   c.gridx++;
                  
                   panel.add(button,c);
                   panelList.add(panel);

                   j++;
                }	    
	}
	
        public void showTables()
        {
            this.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx=0;
                c.gridy=0;
            for(JPanel p: panelList)
            {
                
                
                this.add(p,c);
                c.gridy++;
            }
        }
        
        public Trip getChosenTrip()
        {
            return chosenTrip;
        }
        
       
	public void actionPerformed(ActionEvent e) 
	{
            System.out.println("mmm!!! "+e.getActionCommand());
            String tripid=e.getActionCommand();
            chosenTrip=buttonMap.get(tripid);
                     
            controller.tripChosen(chosenTrip);
            
            
	}
}